{extend name="$layout"}
{block name="title"}用户管理{/block}
{block name="search"}
<div class="layui-form toolbar">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label w-auto">用户名：</label>
            <div class="layui-input-inline">
                <input name="username" class="layui-input" type="text" placeholder="输入用户名"/>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label w-auto">邮箱：</label>
            <div class="layui-input-inline mr0">
                <input name="email" class="layui-input" type="text" placeholder="输入邮箱"/>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label w-auto">状态：</label>
            <div class="layui-input-inline mr0">
                <select name="status">
                    <option value="">选择状态</option>
                    <option value="1">正常</option>
                    <option value="2">禁用</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label w-auto">回收站：</label>
            <div class="layui-input-inline mr0">
                <select name="trash">
                    <option value="">选择</option>
                    <option value="1">恢复数据</option>
                </select>
            </div>
        </div>
        <div class="layui-inline" style="padding-right: 110px;">
            <button class="layui-btn icon-btn" lay-filter="formSubSearchUser" lay-submit>
                <i class="layui-icon">&#xe615;</i>搜索
            </button>
            <button id="btnAddUser" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
        </div>
    </div>
</div>
{/block}
{block name="table"}
<table class="layui-table" id="tableUser" lay-filter="tableUser"></table>
<!-- 表格操作列 -->
<script type="text/html" id="tableBarUser">
    {:editButton()}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="{{d.deleted_at ? 'recover' : 'del'}}">{{d.deleted_at ? '恢复' : '删除'}}</a>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="tableStateUser">
    <input type="checkbox" lay-filter="ckStateUser" value="{{d.id}}" lay-skin="switch"
           lay-text="正常|禁用" {{d.status==1?'checked':''}}/>
</script>
{/block}
{block name="script"}
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;

        // 渲染表格
        var insTb = table.render({
            elem: '#tableUser',
            url: '{:url("users")}',
            page: true,
            response: {
                statusCode: 10000,
            },
            toolbar: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'id', title: '#', field: 'id'},
                {field: 'username', sort: true, title: '用户名'},
                {field: 'email', sort: true, title: '邮箱'},
                {field: 'status', sort: true, title: '状态', templet: '#tableStateUser'},
                {
                    field: 'created_at', sort: true, templet: function (d) {
                        return util.toDateString(d.created_at);
                    }, title: '创建时间'
                },
                {
                    field: 'updated_at', sort: true, templet: function (d) {
                        return util.toDateString(d.updated_at);
                    }, title: '更新时间'
                },
                {align: 'center', toolbar: '#tableBarUser', title: '操作', minWidth: 200}
            ]],
        });

        // 添加
        $('#btnAddUser').click(function () {
            showEditModel();
        });

        // 搜索
        form.on('submit(formSubSearchUser)', function (data) {
            insTb.reload({where: data.field}, 'data');
        });

        // 工具条点击事件
        table.on('tool(tableUser)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'del') { // 删除
                doDel(data.id, data.username);
            } else if (layEvent === 'reset') { // 重置密码
                resetPsw(data.id, data.username);
            } else if (layEvent === 'recover') {
                recover(data.id, data.username);
            }

        });

        function recover(uid, username) {
            layer.confirm('确定要恢复“' + username + '”吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                admin.req('/user/recover/'+ uid,{}, function(res){
                    layer.closeAll('loading');
                    if (res.code == 10000) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({}, 'data');
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'put');
            });
        }
        // 显示表单弹窗
        function showEditModel(mUser) {
            var layIndex = admin.open({
                title: (mUser ? '修改' : '添加') + '用户',
                url: mUser ? '/user/'+mUser.id + '/edit':'/user/create',
                data: mUser,     // 传递数据到表单页面
                area: '500px',
                end: function () {
                    if (admin.getLayerData(layIndex, 'formOk')) {  // 判断表单操作成功标识
                        insTb.reload();  // 成功刷新表格
                    }
                },
                success: function (layero, dIndex) {
                    // 弹窗超出范围不出现滚动条
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                }
            });
        }

        // 删除
        function doDel(userId, nickName) {
            layer.confirm('确定要删除“' + nickName + '”吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                admin.req('/user/'+ userId,{}, function(res){
                    layer.closeAll('loading');
                    if (res.code == 10000) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({}, 'data');
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'delete');
            });
        }

        // 修改用户状态
        form.on('switch(ckStateUser)', function (obj) {
            admin.req('/user/switch/status/'+obj.value,{}, function(res){
                layer.closeAll('loading');
                if (res.code == 10000) {
                    layer.msg(res.msg, {icon: 1});
                } else {
                    layer.msg(res.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'put');
        });
    });
</script>
{/block}